import { db } from '@/db'
import { videos } from '@/db/schema'
import { protectedProcedure, router } from '@/trpc/init'
import { TRPCError } from '@trpc/server'
import { eq, and, or, lt, desc } from 'drizzle-orm'
import { z } from 'zod'

export const studioRouter = router({
  getOne: protectedProcedure
    .input(z.object({ id: z.string().uuid() }))
    .query(async ({ ctx, input }) => {
      const { id } = input
      const { id: userId } = ctx.user

      const [video] = await db
        .select()
        .from(videos)
        .where(and(eq(videos.id, id), eq(videos.userId, userId)))

      if (!video) {
        throw new TRPCError({
          code: 'NOT_FOUND',
          message: 'Video not found'
        })
      }

      return video
    }),
  getMany: protectedProcedure
    .input(
      z.object({
        cursor: z
          .object({
            id: z.string().uuid(),
            updatedAt: z.date()
          })
          .nullish(),
        limit: z.number().min(1).max(100)
      })
    )
    .query(async ({ ctx, input }) => {
      const { cursor, limit } = input
      const { id: userId } = ctx.user

      const data = await db
        .select()
        .from(videos)
        .where(
          and(
            eq(videos.userId, userId),
            cursor
              ? or(
                  lt(videos.id, cursor.id),
                  and(
                    eq(videos.id, cursor.id),
                    lt(videos.updatedAt, cursor.updatedAt)
                  )
                )
              : undefined
          )
        )
        .orderBy(desc(videos.id), desc(videos.updatedAt))
        .limit(limit + 1)

      const hasMore = data.length > limit
      const items = hasMore ? data.slice(0, -1) : data
      const lastItem = items[items.length - 1]
      const nextCursor = hasMore
        ? {
            id: lastItem.id,
            updatedAt: lastItem.updatedAt
          }
        : null

      return {
        items,
        nextCursor
      }
    })
})
